jetcrab\vm\types/
sizes.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
5pub struct MemorySize(usize);
6
7impl MemorySize {
8    pub fn new(size: usize) -> Self {
9        Self(size)
10    }
11
12    pub fn as_usize(&self) -> usize {
13        self.0
14    }
15
16    pub fn is_zero(&self) -> bool {
17        self.0 == 0
18    }
19
20    pub fn add(&self, other: MemorySize) -> Self {
21        Self(self.0 + other.0)
22    }
23
24    pub fn sub(&self, other: MemorySize) -> Self {
25        Self(self.0.saturating_sub(other.0))
26    }
27}
28
29impl From<usize> for MemorySize {
30    fn from(size: usize) -> Self {
31        Self(size)
32    }
33}
34
35impl From<MemorySize> for usize {
36    fn from(size: MemorySize) -> Self {
37        size.0
38    }
39}
40
41impl std::ops::Add for MemorySize {
42    type Output = Self;
43
44    fn add(self, other: Self) -> Self {
45        Self(self.0 + other.0)
46    }
47}
48
49impl std::ops::Sub for MemorySize {
50    type Output = Self;
51
52    fn sub(self, other: Self) -> Self {
53        Self(self.0.saturating_sub(other.0))
54    }
55}
56
57impl fmt::Display for MemorySize {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        write!(f, "{} bytes", self.0)
60    }
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
64pub struct AllocationCount(usize);
65
66impl AllocationCount {
67    pub fn new(count: usize) -> Self {
68        Self(count)
69    }
70
71    pub fn as_usize(&self) -> usize {
72        self.0
73    }
74
75    pub fn increment(&mut self) {
76        self.0 += 1;
77    }
78
79    pub fn reset(&mut self) {
80        self.0 = 0;
81    }
82}
83
84impl From<usize> for AllocationCount {
85    fn from(count: usize) -> Self {
86        Self(count)
87    }
88}
89
90impl From<AllocationCount> for usize {
91    fn from(count: AllocationCount) -> Self {
92        count.0
93    }
94}
95
96impl fmt::Display for AllocationCount {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        write!(f, "{}", self.0)
99    }
100}
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
103pub struct NodeCount(usize);
104
105impl NodeCount {
106    pub fn new(count: usize) -> Self {
107        Self(count)
108    }
109
110    pub fn as_usize(&self) -> usize {
111        self.0
112    }
113
114    pub fn increment(&mut self) {
115        self.0 += 1;
116    }
117
118    pub fn reset(&mut self) {
119        self.0 = 0;
120    }
121}
122
123impl From<usize> for NodeCount {
124    fn from(count: usize) -> Self {
125        Self(count)
126    }
127}
128
129impl From<NodeCount> for usize {
130    fn from(count: NodeCount) -> Self {
131        count.0
132    }
133}
134
135impl fmt::Display for NodeCount {
136    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137        write!(f, "{}", self.0)
138    }
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
142pub struct IndentLevel(usize);
143
144impl IndentLevel {
145    pub fn new(level: usize) -> Self {
146        Self(level)
147    }
148
149    pub fn as_usize(&self) -> usize {
150        self.0
151    }
152
153    pub fn increment(&mut self) {
154        self.0 += 1;
155    }
156
157    pub fn decrement(&mut self) {
158        if self.0 > 0 {
159            self.0 -= 1;
160        }
161    }
162
163    pub fn reset(&mut self) {
164        self.0 = 0;
165    }
166}
167
168impl From<usize> for IndentLevel {
169    fn from(level: usize) -> Self {
170        Self(level)
171    }
172}
173
174impl From<IndentLevel> for usize {
175    fn from(level: IndentLevel) -> Self {
176        level.0
177    }
178}
179
180impl fmt::Display for IndentLevel {
181    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182        write!(f, "{}", self.0)
183    }
184}
185
186impl std::ops::AddAssign<usize> for IndentLevel {
187    fn add_assign(&mut self, rhs: usize) {
188        self.0 += rhs;
189    }
190}
191
192impl std::ops::SubAssign<usize> for IndentLevel {
193    fn sub_assign(&mut self, rhs: usize) {
194        self.0 = self.0.saturating_sub(rhs);
195    }
196}
197
198#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
199pub struct ObjectCount(usize);
200
201impl ObjectCount {
202    pub fn new(count: usize) -> Self {
203        Self(count)
204    }
205
206    pub fn as_usize(&self) -> usize {
207        self.0
208    }
209
210    pub fn is_zero(&self) -> bool {
211        self.0 == 0
212    }
213
214    pub fn is_positive(&self) -> bool {
215        self.0 > 0
216    }
217
218    pub fn increment(&mut self) {
219        self.0 += 1;
220    }
221
222    pub fn decrement(&mut self) {
223        if self.0 > 0 {
224            self.0 -= 1;
225        }
226    }
227
228    pub fn add(&self, other: ObjectCount) -> Self {
229        Self(self.0 + other.0)
230    }
231
232    pub fn sub(&self, other: ObjectCount) -> Self {
233        Self(self.0.saturating_sub(other.0))
234    }
235}
236
237impl From<usize> for ObjectCount {
238    fn from(count: usize) -> Self {
239        Self(count)
240    }
241}
242
243impl From<ObjectCount> for usize {
244    fn from(count: ObjectCount) -> Self {
245        count.0
246    }
247}
248
249impl std::ops::Add for ObjectCount {
250    type Output = Self;
251
252    fn add(self, other: Self) -> Self {
253        Self(self.0 + other.0)
254    }
255}
256
257impl std::ops::Sub for ObjectCount {
258    type Output = Self;
259
260    fn sub(self, other: Self) -> Self {
261        Self(self.0.saturating_sub(other.0))
262    }
263}
264
265impl fmt::Display for ObjectCount {
266    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
267        write!(f, "{}", self.0)
268    }
269}
270
271#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
272pub struct ScopeDepth(usize);
273
274impl ScopeDepth {
275    pub fn new(depth: usize) -> Self {
276        Self(depth)
277    }
278
279    pub fn as_usize(&self) -> usize {
280        self.0
281    }
282
283    pub fn is_zero(&self) -> bool {
284        self.0 == 0
285    }
286
287    pub fn is_positive(&self) -> bool {
288        self.0 > 0
289    }
290
291    pub fn increment(&mut self) {
292        self.0 += 1;
293    }
294
295    pub fn decrement(&mut self) {
296        if self.0 > 0 {
297            self.0 -= 1;
298        }
299    }
300
301    pub fn add(&self, other: ScopeDepth) -> Self {
302        Self(self.0 + other.0)
303    }
304
305    pub fn sub(&self, other: ScopeDepth) -> Self {
306        Self(self.0.saturating_sub(other.0))
307    }
308}
309
310impl From<usize> for ScopeDepth {
311    fn from(depth: usize) -> Self {
312        Self(depth)
313    }
314}
315
316impl From<ScopeDepth> for usize {
317    fn from(depth: ScopeDepth) -> Self {
318        depth.0
319    }
320}
321
322impl std::ops::Add for ScopeDepth {
323    type Output = Self;
324
325    fn add(self, other: Self) -> Self {
326        Self(self.0 + other.0)
327    }
328}
329
330impl std::ops::Sub for ScopeDepth {
331    type Output = Self;
332
333    fn sub(self, other: Self) -> Self {
334        Self(self.0.saturating_sub(other.0))
335    }
336}
337
338impl fmt::Display for ScopeDepth {
339    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
340        write!(f, "{}", self.0)
341    }
342}
343
344#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
345pub struct VariableCount(usize);
346
347impl VariableCount {
348    pub fn new(count: usize) -> Self {
349        Self(count)
350    }
351
352    pub fn as_usize(&self) -> usize {
353        self.0
354    }
355
356    pub fn is_zero(&self) -> bool {
357        self.0 == 0
358    }
359
360    pub fn is_positive(&self) -> bool {
361        self.0 > 0
362    }
363
364    pub fn increment(&mut self) {
365        self.0 += 1;
366    }
367
368    pub fn decrement(&mut self) {
369        if self.0 > 0 {
370            self.0 -= 1;
371        }
372    }
373
374    pub fn add(&self, other: VariableCount) -> Self {
375        Self(self.0 + other.0)
376    }
377
378    pub fn sub(&self, other: VariableCount) -> Self {
379        Self(self.0.saturating_sub(other.0))
380    }
381}
382
383impl From<usize> for VariableCount {
384    fn from(count: usize) -> Self {
385        Self(count)
386    }
387}
388
389impl From<VariableCount> for usize {
390    fn from(count: VariableCount) -> Self {
391        count.0
392    }
393}
394
395impl std::ops::Add for VariableCount {
396    type Output = Self;
397
398    fn add(self, other: Self) -> Self {
399        Self(self.0 + other.0)
400    }
401}
402
403impl std::ops::Sub for VariableCount {
404    type Output = Self;
405
406    fn sub(self, other: Self) -> Self {
407        Self(self.0.saturating_sub(other.0))
408    }
409}
410
411impl fmt::Display for VariableCount {
412    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
413        write!(f, "{}", self.0)
414    }
415}
416
417#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
418pub struct ObjectSize(usize);
419
420impl ObjectSize {
421    pub fn new(size: usize) -> Self {
422        Self(size)
423    }
424
425    pub fn as_usize(&self) -> usize {
426        self.0
427    }
428
429    pub fn is_zero(&self) -> bool {
430        self.0 == 0
431    }
432
433    pub fn is_positive(&self) -> bool {
434        self.0 > 0
435    }
436
437    pub fn add(&self, other: ObjectSize) -> Self {
438        Self(self.0 + other.0)
439    }
440
441    pub fn sub(&self, other: ObjectSize) -> Self {
442        Self(self.0.saturating_sub(other.0))
443    }
444}
445
446impl From<usize> for ObjectSize {
447    fn from(size: usize) -> Self {
448        Self(size)
449    }
450}
451
452impl From<ObjectSize> for usize {
453    fn from(size: ObjectSize) -> Self {
454        size.0
455    }
456}
457
458impl std::ops::Add for ObjectSize {
459    type Output = Self;
460
461    fn add(self, other: Self) -> Self {
462        Self(self.0 + other.0)
463    }
464}
465
466impl std::ops::Sub for ObjectSize {
467    type Output = Self;
468
469    fn sub(self, other: Self) -> Self {
470        Self(self.0.saturating_sub(other.0))
471    }
472}
473
474impl fmt::Display for ObjectSize {
475    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
476        write!(f, "{}", self.0)
477    }
478}
479
480#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
481pub struct ErrorCount(usize);
482
483impl ErrorCount {
484    pub fn new(count: usize) -> Self {
485        Self(count)
486    }
487
488    pub fn as_usize(&self) -> usize {
489        self.0
490    }
491
492    pub fn is_zero(&self) -> bool {
493        self.0 == 0
494    }
495
496    pub fn is_positive(&self) -> bool {
497        self.0 > 0
498    }
499
500    pub fn increment(&mut self) {
501        self.0 += 1;
502    }
503
504    pub fn decrement(&mut self) {
505        if self.0 > 0 {
506            self.0 -= 1;
507        }
508    }
509
510    pub fn reset(&mut self) {
511        self.0 = 0;
512    }
513
514    pub fn add(&self, other: ErrorCount) -> Self {
515        Self(self.0 + other.0)
516    }
517
518    pub fn sub(&self, other: ErrorCount) -> Self {
519        Self(self.0.saturating_sub(other.0))
520    }
521}
522
523impl From<usize> for ErrorCount {
524    fn from(count: usize) -> Self {
525        Self(count)
526    }
527}
528
529impl From<ErrorCount> for usize {
530    fn from(count: ErrorCount) -> Self {
531        count.0
532    }
533}
534
535impl std::ops::Add for ErrorCount {
536    type Output = Self;
537
538    fn add(self, other: Self) -> Self {
539        Self(self.0 + other.0)
540    }
541}
542
543impl std::ops::Sub for ErrorCount {
544    type Output = Self;
545
546    fn sub(self, other: Self) -> Self {
547        Self(self.0.saturating_sub(other.0))
548    }
549}
550
551impl std::ops::AddAssign for ErrorCount {
552    fn add_assign(&mut self, rhs: Self) {
553        self.0 += rhs.0;
554    }
555}
556
557impl fmt::Display for ErrorCount {
558    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
559        write!(f, "{}", self.0)
560    }
561}